package com.itnoob.clone;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhoujun
 * @date 2025/3/21 22:44
 */
public class Folder extends FileSystem{
    /**
     * 定义一个list属性,用来存储子节点
     */
    protected List<FileSystem> children;

    public Folder(String name) {
        this.name = name;
        this.children = new ArrayList<>();
    }

    /**
     * 实现display方法,打印文件夹和子节点的信息
     */
    @Override
    public void disPlay() {
        System.out.println("Folder: " + name);
        for (FileSystem child : children) {
            child.disPlay();
        }
    }

    /**
     * 添加一个子节点
     * @param child
     */
    public void add(FileSystem child){
        children.add(child);
    }

    /**
     * 删除一个子节点
     * @param child
     */
    public void remove(FileSystem child){
        children.remove(child);
    }


    /**
     * 重写clone方法,向其中递归的复制节点
     * @return
     * @throws CloneNotSupportedException
     */
    @Override
    protected Object clone() throws CloneNotSupportedException {
        Folder folder = null;
        folder = (Folder)super.clone();  //复制当前对象
        folder.children = new ArrayList<>();
        for (FileSystem child : this.children) {
            FileSystem  cloneChild =  (FileSystem) child.clone();
            folder.children.add(cloneChild);
        }
        return folder;
    }
}
